package com.demo2do.fms.admin.web.controller.supplier.sub.flow;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.export.supplier.VehicleSourceSheet;
import com.demo2do.fms.admin.entity.search.supplier.VehicleSourceSearch;
import com.demo2do.fms.admin.service.supplier.VehicleSourceService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;

/**
 * 供方车辆申报审核
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/vehicle-declare-audits")
@MenuSetting("menu-supplier")
public class VehicleDeclareAuditController {
    
    private static final Logger  logger = LoggerFactory.getLogger(VehicleDeclareAuditController.class);
                                        
    @Autowired
    private VehicleSourceService vehicleSourceService;
                                 
    /**
     * 供方车辆审核列表
     * 
     * @param syncStatus
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-auditing-vehicle-management")
    public ModelAndView list(HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-list");
                
        int totals = vehicleSourceService.countDeclare();
        if ( totals > 0 ) {
            
            List<VehicleSource> vehicleSources = vehicleSourceService.listDeclare(page.with(totals));
            modelAndView.addObject("vehicleSources", vehicleSources);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 过滤查询panel
     * 
     * @param syncStatus
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-auditing-vehicle-management")
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-search");
        return modelAndView;
    }
    
    /**
     * 过滤查询
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-auditing-vehicle-management")
    public ModelAndView onSearch(VehicleSourceSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-list");
        
        int totals = vehicleSourceService.count(search);
        if (totals > 0) {
            
            List<VehicleSource> vehicleSources = vehicleSourceService.search(search, page.with(totals));
            modelAndView.addObject("vehicleSources", vehicleSources);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 标记同步
     * 
     * @param vehicleId
     * @return
     */
    @RequestMapping(value = "/{vehicleId}/sign", method = RequestMethod.GET)
    public ModelAndView sign(@PathVariable("vehicleId") Long vehicleId) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-sign");
                
        modelAndView.addObject("vehicleId", vehicleId);
        
        return modelAndView;
        
    }
    
    /**
     * on sign
     * 
     * @param vehicleId
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{vehicleId}/sign", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSign(@PathVariable("vehicleId") Long vehicleId,
                                           @Secure(property = "id") Long operator) {
            
        HttpResult httpResult = new HttpResult();
        
        try {
            
            vehicleSourceService.sign(vehicleId, operator);
            
            httpResult.success().message("标记同步成功");
            
        } catch (Exception e) {
            logger.error("#onSign() - sign fail, exception = [{}]", e);
            httpResult.fail("500").message("标记同步失败！" + e.getMessage());
        }
        
        return httpResult;
        
    }
    
    /**
     * 批量标记已同步panel
     * 
     * @return
     */
    @RequestMapping(value = "/batch-sign", method = RequestMethod.GET)
    @MenuSetting("submenu-auditing-vehicle-management")
    public ModelAndView batchSign() {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-batch-sign");
        return modelAndView;
    }
    
    /**
     * 批量标记已同步
     * 
     * @param operator
     * @param vehicleIds
     * @return
     */
    @RequestMapping(value = "/batch-sign", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-auditing-vehicle-management")
    public @ResponseBody HttpResult onBatchSign(@RequestParam("vehicleIds") List<Long> vehicleIds,
                                                @Secure(property = "id") Long operator) {
            
        HttpResult httpResult = new HttpResult();
        
        try {
            
            int failNumber = vehicleSourceService.batchSign(vehicleIds, operator);
            
            if (failNumber > 0) {
                httpResult.fail("500").message("批量标记已同步成功，成功数量：" + (vehicleIds.size() - failNumber) + "，失败数量：" + failNumber);
            } else {
                httpResult.success().message("批量标记已同步成功");
            }
            
        } catch (Exception e) {
            logger.error("#onBatchSign() - batch sign fail, exception = [{}]", e);
            httpResult.fail("500").message("批量标记已同步失败");
        }
        
        return httpResult;
        
    }
    
    /**
     * 驳回panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/reject", method = RequestMethod.GET)
    public ModelAndView reject(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-reject");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }

    /**
     * 驳回
     * 
     * @param id
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/reject", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onReject(@Preloading("id") VehicleSource vehicleSource, 
                                             @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();

        try {

            vehicleSourceService.reject(vehicleSource, operator);
            
            httpResult.success().message("驳回成功");

        } catch (Exception e) {
            logger.error("#onReject() - reject fail, exception = [{}]", e);
            httpResult.fail("500").message("驳回失败");
        }

        return httpResult;

    }
    
    /**
     * 导出panel
     * 
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/vehicle-declare/vehicle-declare-audit-export");
        return modelAndView;
    }

    /**
     * 导出
     * 
     * @param operator
     * @param vehicleIds
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public ModelAndView onExport(@Secure(property = "id") Long operator,
                                 @RequestParam("vehicleIds") List<Long> vehicleIds) {
            
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("supplier/vehicle-source-export.xlsx"));
        
        List<VehicleSourceSheet> vehicleSourceSheets = vehicleSourceService.generateExport(vehicleIds);
        modelAndView.addObject("vehicleSources", vehicleSourceSheets);
        
        return modelAndView;
        
    }
    
}
